<template>
  <CustomNav title="修改个性签名" />
  <view class="content">
    <u-input
      placeholder="请输入个性签名"
      v-model="signature"
      maxlength="10"
      @change="change"
    ></u-input>
  </view>

  <view class="default-button" @click="handleUpdate">确认保存</view>
</template>

<script setup>
import CustomNav from "@/components/CustomNav/index.vue";
import { Alert, unAlert, chineseReg } from "../../utils/tool";
import { updateUserData } from "@/api/user";
import { getItem, setItem, updateItem } from "../../utils/localStorage";

onMounted(() => {
  // signature.value = getItem("userInfo").description;
});

const signature = ref("");
const change = (e) => {
  signature.value = e;
};

const handleUpdate = async () => {
  if (!signature.value) {
    Alert("请输入个性签名");
    return;
  } else if (getItem("userInfo").description == signature.value) {
    Alert("个性签名与之前一致！");
    return;
  }

  Alert("加载中", "loading", 0, true);

  const res = await updateUserData({
    id: getItem("userInfo").id,
    description: signature.value,
  });

  if (res.data.code == 200) {
    Alert("修改成功", "success");
    updateItem("userInfo", "description", res.data.data.description);
    setTimeout(() => {
      uni.navigateBack();
    }, 700);
  }

  console.log("res：", res);
};
</script>
<script>
export default {
  name: "",
};
</script>

<style lang="scss" scoped>
.content {
  border-bottom: 1px solid #ccc;
  margin: 20px;
}
:deep(.u-input) {
  // border: 1px solid red;
}
</style>
